<?php
namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\common\model\Gameagent;
use app\common\model\Order;
use app\common\model\Stat as Stats;
use think\facade\Session;

/**
 * 后台首页
 * Class Index
 * @package app\admin\controller
 */
class Finance extends AdminBase
{
    protected function initialize()
    {
        parent::initialize();
    }

    /**
     * 数据统计 月份
     */
    public function months()
    {
        $params = $this->request->param();
        $page = $params['page'] ?? 1;

        $stat = new Stats();
        $where = [];
        if(!empty($params['gid'])){
            $where[] = ['gid', '=', $params['gid']];
        }else{
            $where[] = ['gid', '>', '0'];
        }
        $where[] = ['cid', '=', 5];

        if (!empty($params['starttime']) && !empty($params['endtime'])) {
            $where[]= ['stat_date','between time',[$params['starttime'],$params['endtime']]];
        }elseif (!empty($params['starttime'])) {
            $where[] = ['stat_date','>=',$params['starttime']];
        }elseif (!empty($params['endtime'])) {
            $where[] = ['stat_date','<=',$params['endtime']];
        }else{
            // 默认当前月份
            $starttime = date("Y-m")."-01";
            $endtime = date("Y-m-d");
//            $endtime = date("Y-m")."-".date("t");
            $where[]= ['stat_date','between',[$starttime, $endtime]];
        }

        $list = $stat->alias('s')->field("s.gid,DATE_FORMAT(s.stat_date, '%Y-%m') as month_date,sum(s.new_users) as new_users,sum(s.new_roles) as new_roles,
            sum(s.active_users) as active_users,sum(s.pay_amount) as pay_amount,sum(s.pay_users) as pay_users,sum(s.pay_times) as pay_times,
            ROUND( SUM( s.pay_amount ) / NULLIF( SUM( s.new_users ), 0 ), 2 ) AS arpu,
            ROUND( SUM( s.pay_amount ) / NULLIF( sum( s.pay_users ), 0 ), 2 ) AS arppu,
            g.name,g.gchannel")
            ->leftJoin('tab_gamelist g','s.gid=g.id')
            ->where($where)
            ->group("s.gid")
            ->order("month_date desc")
            ->paginate(100);
        // 初始化求和变量
        $sumData = [
            'new_users' => 0,
            'new_roles' => 0,
            'active_users' => 0,
            'pay_amount' => 0,
            'pay_users' => 0,
            'pay_times' => 0,
            'arpu' => 0,
            'arppu' => 0,
        ];

        if(!empty($list)){
            foreach ($list as $val){
                $sumData['new_users'] += $val['new_users'];
                $sumData['new_roles'] += $val['new_roles'];
                $sumData['active_users'] += $val['active_users'];
                $sumData['pay_amount'] += $val['pay_amount'];
                $sumData['pay_users'] += $val['pay_users'];
                $sumData['pay_times'] += $val['pay_times'];
            }
            $sumData['arpu'] = $sumData['active_users'] > 0 ? round($sumData['pay_amount']/$sumData['active_users'],2) : 0;
            $sumData['arppu'] = $sumData['pay_users'] > 0 ? round($sumData['pay_amount']/$sumData['pay_users'],2) : 0;
        }

        $this->assign("sumData",$sumData);

        $game_list = get_game_list();
        $this->assign("game_list",$game_list);
        $this->assign("action","months");
        $this->assign("counts",count($list));

        // 转换为数组以便返回
        return $this->fetch('months', ['list' => $list]);
    }


    /**
     * 结算账单
     * @return list
     * 按照月份统计结算
     */
     public function order(){
         $params = $this->request->param();
         $page = $params['page'] ?? 1;

         $stat = new Stats();
         $where = [];
         if(!empty($params['gid'])){
             $where[] = ['gid', '=', $params['gid']];
         }else{
             $where[] = ['gid', '>', '0'];
         }
         $where[] = ['cid', '=', 5];

         if (!empty($params['starttime']) && !empty($params['endtime'])) {
             $where[]= ['stat_date','between time',[$params['starttime'],$params['endtime']]];
         }elseif (!empty($params['starttime'])) {
             $where[] = ['stat_date','>=',$params['starttime']];
         }elseif (!empty($params['endtime'])) {
             $where[] = ['stat_date','<=',$params['endtime']];
         }else{
             // 默认当前月份
             $starttime = date("Y-m")."-01";
             $endtime = date("Y-m-d");
//            $endtime = date("Y-m")."-".date("t");
             $where[]= ['stat_date','between',[$starttime, $endtime]];
         }

         $list = $stat->alias('s')->field("s.gid,DATE_FORMAT(s.stat_date, '%Y-%m') as month_date,sum(s.new_users) as new_users,sum(s.new_roles) as new_roles,
            sum(s.active_users) as active_users,sum(s.pay_amount) as pay_amount,sum(s.pay_users) as pay_users,sum(s.pay_times) as pay_times,
            ROUND( SUM( s.pay_amount ) / NULLIF( SUM( s.new_users ), 0 ), 2 ) AS arpu,
            ROUND( SUM( s.pay_amount ) / NULLIF( sum( s.pay_users ), 0 ), 2 ) AS arppu,
            g.name,g.gchannel")
             ->leftJoin('tab_gamelist g','s.gid=g.id')
             ->where($where)
             ->group("s.gid")
             ->order("month_date desc")
             ->paginate(100);
         // 初始化求和变量
         $sumData = [
             'new_users' => 0,
             'new_roles' => 0,
             'active_users' => 0,
             'pay_amount' => 0,
             'pay_users' => 0,
             'pay_times' => 0,
             'arpu' => 0,
             'arppu' => 0,
         ];

         if(!empty($list)){
             foreach ($list as $val){
                 $sumData['new_users'] += $val['new_users'];
                 $sumData['new_roles'] += $val['new_roles'];
                 $sumData['active_users'] += $val['active_users'];
                 $sumData['pay_amount'] += $val['pay_amount'];
                 $sumData['pay_users'] += $val['pay_users'];
                 $sumData['pay_times'] += $val['pay_times'];
             }
             $sumData['arpu'] = $sumData['active_users'] > 0 ? round($sumData['pay_amount']/$sumData['active_users'],2) : 0;
             $sumData['arppu'] = $sumData['pay_users'] > 0 ? round($sumData['pay_amount']/$sumData['pay_users'],2) : 0;
         }

         $this->assign("sumData",$sumData);

         $game_list = get_game_list();
         $this->assign("game_list",$game_list);
         $this->assign("action","months");
         $this->assign("counts",count($list));

         // 转换为数组以便返回
         return $this->fetch('months', ['list' => $list]);
     }


}